﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Clients_BookTicket_2 : System.Web.UI.Page
{
    ListAndProcessData ws = new ListAndProcessData();
    protected void Page_Load(object sender, EventArgs e)
    {
        string getseatuse = Request.QueryString["seatused"];
        txtseatused.Value = getseatuse;
       
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "AddSelect", " addselect(1);", true);
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "AddAge", " addage(1);", true);
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "removeitem", " removeitem('" + getseatuse + "',1);", true);


    }
    protected void btncontinute_Click(object sender, EventArgs e)
    {
        
        int idcusLast = 0;
        int idbusticket = 0;
        int pricebustype = 0;
        int getidbuscar = Int32.Parse(Request.QueryString["idbuscar"]);
        string getidbustype = Request.QueryString["idbustype"];
        int totalseat = 0;
        idcusLast = InsertAndGetidCustomer(txtFullNameCustomer.Text,txtAge.Text);
        idbusticket = InsertAndGetidBusTicket(1, idcusLast);
        
        
        var getprice = ws.getBusType();
        foreach(var item in getprice)
        {
            if(Int32.Parse(getidbustype) == item.IDBusType)
            {
                pricebustype = Int32.Parse(item.Price.ToString());
                totalseat = Int32.Parse(item.TotalSeats.ToString());
            }
            
        }

        int sumprice = 0;
        int sum = 0;
        string string1 = txtallelement.Value;
        string[] groupstring = string1.Split(';');

        foreach (string string2 in groupstring)
        {
            string[] eachstring = string2.Split(',');
            string getname = eachstring[0];
            string getseat = eachstring[1].ToString();
            string getage = eachstring[2];
            idcusLast = InsertAndGetidCustomer(getname,getage);
            sumprice = InsertBusticketdetail(getage, idbusticket, getidbuscar, idcusLast, pricebustype, getseat);
            ws.UpdateSeatUsedBusCard(getidbuscar, getseat);
            sum = sum + sumprice;
        }
        ws.UpdatePriceBusTicket(idbusticket, sum);

        var countseatused = ws.getBusCarWithId(getidbuscar);
        string stringseatused = null;
        foreach (var item in countseatused)
        {
            stringseatused = item.SeatsUsed;
        }

        string[] countstring = stringseatused.Split(',');
        int count = 0;
        foreach (string string2 in countstring)
        {
            count = count + 1;
        }

        if (count == totalseat)
        {
            ws.UpdateStatusBuscar(getidbuscar, 1);
        }
    }

    public int InsertAndGetidCustomer(string getname1,string getage1)
    {
        int idcus = 0;
        if (DropDownList1.SelectedValue == "Male")
        {
            ws.InsertCustomer(getname1, getage1, 0, txtAddress.Text, txtPhone.Text, txtEmail.Text, txtCMND.Text);
        }
        if (DropDownList1.SelectedValue == "FeMale")
        {
            ws.InsertCustomer(getname1, getage1, 1, txtAddress.Text, txtPhone.Text, txtEmail.Text, txtCMND.Text);
        }

        var getnewidcus = ws.getAllCustomer();
        foreach (var item in getnewidcus)
        {
            idcus = item.Idcustomer;
        }
        return idcus;
    }

    public int InsertBusticketdetail(string getage,int idbusticket,int idbuscar,int idcustomer,int txtprice,string txtseat)
    {
        int idsumprice = 0;
        if (getage == "5")
        {
            ws.InsertBusTicketDetail(idbusticket, idbuscar, idcustomer, 0, txtseat);
           
        }
        else if (getage == "512")
        {
            idsumprice = txtprice / 2;
            ws.InsertBusTicketDetail(idbusticket, idbuscar, idcustomer, idsumprice, txtseat);
         
        }
        else if (getage == "1250")
        {
            idsumprice = txtprice;
            ws.InsertBusTicketDetail(idbusticket, idbuscar, idcustomer, txtprice, txtseat);
           
        }
        else if (getage == "50")
        {
            idsumprice = txtprice - (txtprice * 30 / 100);
            ws.InsertBusTicketDetail(idbusticket, idbuscar, idcustomer, idsumprice, txtseat);
        }

        return idsumprice;
    }

    public int InsertAndGetidBusTicket(int idemployee,int idcustomer)
    {
        int idbustic = 0;
        ws.InsertBusticket(idemployee, idcustomer);
        var getidnewbusticket = ws.getbusticket();
        foreach (var item in getidnewbusticket)
        {
            idbustic = item.IdBusTicket;
        }
        return idbustic;
    }
}